home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / xvisrc.zip / CMDLINE.C < prev    next >
C/C++ Source or Header  |  1992-07-28  |  27KB  |  1,256 lines

  1. /* Copyright (c) 1990,1991,1992 Chris and John Downey */
  2. #ifndef lint
  3. static char *sccsid = "@(#)cmdline.c    2.2 (Chris & John Downey) 8/6/92";
  4. #endif
  5.  
  6. /***
  7.  
  8. * program name:
  9.     xvi
  10. * function:
  11.     PD version of UNIX "vi" editor, with extensions.
  12. * module name:
  13.     cmdline.c
  14. * module function:
  15.     Command-line handling (i.e. :/? commands) - most
  16.     of the actual command functions are in ex_cmds.c.
  17. * history:
  18.     STEVIE - ST Editor for VI Enthusiasts, Version 3.10
  19.     Originally by Tim Thompson (twitch!tjt)
  20.     Extensive modifications by Tony Andrews    (onecom!wldrdg!tony)
  21.     Heavily modified by Chris & John Downey
  22.  
  23. ***/
  24.  
  25. #include "xvi.h"
  26.  
  27. #ifdef    MEGAMAX
  28. overlay "cmdline"
  29. #endif
  30.  
  31. /*
  32.  * The next two variables contain the bounds of any range
  33.  * given in a command. If no range was given, both will be NULL.
  34.  * If only a single line was given, u_line will be NULL.
  35.  * The a_line variable is used for those commands which take
  36.  * a third line specifier after the command, e.g. "move", "copy".
  37.  */
  38. static    Line    *l_line, *u_line;
  39. static    Line    *a_line;
  40.  
  41. /*
  42.  * Definitions for all ex commands.
  43.  */
  44.  
  45. #define    EX_ENOTFOUND    -1        /* command not found */
  46. #define    EX_EAMBIGUOUS    -2        /* could be more than one */
  47. #define    EX_ECANTFORCE    -3        /* ! given where not appropriate */
  48. #define    EX_EBADARGS    -4        /* inappropriate args given */
  49.  
  50. #define    EX_NOCMD    1
  51. #define    EX_SHCMD    2
  52. #define    EX_UNUSED    3    /* unused */
  53. #define    EX_AMPERSAND    4
  54. #define    EX_EXBUFFER    5
  55. #define    EX_LSHIFT    6
  56. #define    EX_EQUALS    7
  57. #define    EX_RSHIFT    8
  58. #define    EX_COMMENT    9
  59. #define    EX_ABBREVIATE    10
  60. #define    EX_APPEND    11
  61. #define    EX_ARGS        12
  62. #define    EX_BUFFER    13
  63. #define    EX_CHDIR    14
  64. #define    EX_CHANGE    15
  65. #define    EX_CLOSE    16
  66. #define    EX_COMPARE    17
  67. #define    EX_COPY        18
  68. #define    EX_DELETE    19
  69. #define    EX_ECHO        20
  70. #define    EX_EDIT        21
  71. #define    EX_EX        22
  72. #define    EX_FILE        23
  73. #define    EX_GLOBAL    24
  74. #define    EX_HELP        25
  75. #define    EX_INSERT    26
  76. #define    EX_JOIN        27
  77. #define    EX_K        28
  78. #define    EX_LIST        29
  79. #define    EX_MAP        30
  80. #define    EX_MARK        31
  81. #define    EX_MOVE        32
  82. #define    EX_NEXT        33
  83. #define    EX_NUMBER    34
  84. #define    EX_OPEN        35
  85. #define    EX_PRESERVE    36
  86. #define    EX_PRINT    37
  87. #define    EX_PUT        38
  88. #define    EX_QUIT        39
  89. #define    EX_READ        40
  90. #define    EX_RECOVER    41
  91. #define    EX_REWIND    42
  92. #define    EX_SET        43
  93. #define    EX_SHELL    44
  94. #define    EX_SOURCE    45
  95. #define    EX_SPLIT    46
  96. #define    EX_SUSPEND    47
  97. #define    EX_SUBSTITUTE    48
  98. #define    EX_TAG        49
  99. #define    EX_UNABBREV    50
  100. #define    EX_UNDO        51
  101. #define    EX_UNMAP    52
  102. #define    EX_V        53
  103. #define    EX_VERSION    54
  104. #define    EX_VISUAL    55
  105. #define    EX_WN        56
  106. #define    EX_WQ        57
  107. #define    EX_WRITE    58
  108. #define    EX_XIT        59
  109. #define    EX_YANK        60
  110. #define    EX_Z        61
  111. #define    EX_GOTO        62
  112. #define    EX_TILDE    63
  113.  
  114. /*
  115.  * Table of all ex commands, and whether they take an '!'.
  116.  *
  117.  * Note that this table is in strict order, sorted on
  118.  * the ASCII value of the first character of the command.
  119.  *
  120.  * The priority field is necessary to resolve clashes in
  121.  * the first one or two characters; so each group of commands
  122.  * beginning with the same letter should have at least one
  123.  * priority 1, so that there is a sensible default.
  124.  *
  125.  * Commands with argument type ec_rest need no delimiters;
  126.  * they need only be matched. This is really only used for
  127.  * single-character commands like !, " and &.
  128.  */
  129. static    struct    ecmd    {
  130.     char    *ec_name;
  131.     short    ec_command;
  132.     short    ec_priority;
  133.     unsigned    ec_flags;
  134.     /*
  135.      * Flags: EXCLAM means can use !, FILEXP means do filename
  136.      * expansion, INTEXP means do % and # expansion. EXPALL means
  137.      * do INTEXP and FILEXP (they are done in that order).
  138.      *
  139.      * EC_RANGE0 means that the range specifier (if any)
  140.      * may include line 0.
  141.      */
  142. #   define    EC_EXCLAM    0x1
  143. #   define    EC_FILEXP    0x2
  144. #   define    EC_INTEXP    0x4
  145. #   define    EC_EXPALL    EC_FILEXP|EC_INTEXP
  146. #   define    EC_RANGE0    0x8
  147.  
  148.     enum {
  149.     ec_none,        /* no arguments after command */
  150.     ec_strings,        /* whitespace-separated strings */
  151.     ec_1string,        /* like ec_strings but only one */
  152.     ec_line,        /* line number or target argument */
  153.     ec_rest,        /* rest of line passed entirely */
  154.     ec_nonalnum,        /* non-alphanumeric delimiter */
  155.     ec_1lower        /* single lower-case letter */
  156.     }    ec_arg_type;
  157. } cmdtable[] = {
  158. /*  name        command         priority    exclam    */
  159.  
  160.     /*
  161.      * The zero-length string is used for the :linenumber command.
  162.      */
  163.     "",            EX_NOCMD,        1,    EC_RANGE0,        ec_none,
  164.     "!",        EX_SHCMD,        0,    EC_INTEXP,        ec_rest,
  165.  
  166.     "#",        EX_NUMBER,        0,    0,            ec_none,
  167.     "&",        EX_AMPERSAND,   0,    0,            ec_rest,
  168.     "*",        EX_EXBUFFER,    0,    0,            ec_rest,
  169.     "<",        EX_LSHIFT,        0,    0,            ec_none,
  170.     "=",        EX_EQUALS,        0,    0,            ec_none,
  171.     ">",        EX_RSHIFT,        0,    0,            ec_none,
  172.     "@",        EX_EXBUFFER,    0,    0,            ec_rest,
  173.     "\"",        EX_COMMENT,        0,    0,            ec_rest,
  174.  
  175.     "abbreviate",   EX_ABBREVIATE,  0,    0,            ec_strings,
  176.     "append",        EX_APPEND,        1,    0,            ec_none,
  177.     "args",        EX_ARGS,        0,    0,            ec_none,
  178.  
  179.     "buffer",        EX_BUFFER,        0,    EC_EXPALL,        ec_1string,
  180.  
  181.     "cd",        EX_CHDIR,        1,    EC_EXPALL,        ec_1string,
  182.     "change",        EX_CHANGE,        2,    0,            ec_none,
  183.     "chdir",        EX_CHDIR,        1,    EC_EXPALL,        ec_1string,
  184.     "close",        EX_CLOSE,        1,    EC_EXCLAM,        ec_none,
  185.     "compare",        EX_COMPARE,        0,    0,            ec_none,
  186.     "copy",        EX_COPY,        1,    0,            ec_line,
  187.  
  188.     "delete",        EX_DELETE,        0,    0,            ec_none,
  189.  
  190.     "echo",        EX_ECHO,        0,    EC_INTEXP,        ec_strings,
  191.     "edit",        EX_EDIT,        1,    EC_EXCLAM|EC_EXPALL,    ec_1string,
  192.     "ex",        EX_EX,        0,    EC_EXPALL,        ec_1string,
  193.  
  194.     "file",        EX_FILE,        0,    EC_EXPALL,        ec_1string,
  195.  
  196.     "global",        EX_GLOBAL,        0,    EC_EXCLAM,        ec_nonalnum,
  197.  
  198.     "help",        EX_HELP,        0,    0,            ec_none,
  199.  
  200.     "insert",        EX_INSERT,        0,    0,            ec_none,
  201.  
  202.     "join",        EX_JOIN,        0,    0,            ec_none,
  203.  
  204.     "k",        EX_K,        0,    0,            ec_1lower,
  205.  
  206.     "list",        EX_LIST,        0,    0,            ec_none,
  207.  
  208.     "map",        EX_MAP,        0,    EC_EXCLAM,        ec_strings,
  209.     "mark",        EX_MARK,        0,    0,            ec_1lower,
  210.     "move",        EX_MOVE,        1,    0,            ec_line,
  211.  
  212.     "next",        EX_NEXT,        1,    EC_EXCLAM|EC_EXPALL,    ec_strings,
  213.     "number",        EX_NUMBER,        0,    0,            ec_none,
  214.  
  215.     "open",        EX_OPEN,        0,    0,            ec_none,
  216.  
  217.     "preserve",        EX_PRESERVE,    0,    0,            ec_none,
  218.     "print",        EX_PRINT,        1,    0,            ec_none,
  219.     "put",        EX_PUT,        0,    EC_RANGE0,        ec_none,
  220.  
  221.     "quit",        EX_QUIT,        0,    EC_EXCLAM,        ec_none,
  222.  
  223.     "read",        EX_READ,        1,    EC_EXPALL|EC_RANGE0,    ec_1string,
  224.     "recover",        EX_RECOVER,        0,    0,            ec_none,
  225.     "rewind",        EX_REWIND,        0,    EC_EXCLAM,        ec_none,
  226.  
  227.     "set",        EX_SET,        0,    0,            ec_strings,
  228.     "shell",        EX_SHELL,        0,    0,            ec_none,
  229.     "source",        EX_SOURCE,        0,    EC_EXPALL,        ec_1string,
  230.     "split",        EX_SPLIT,        0,    0,            ec_none,
  231.     "stop",        EX_SUSPEND,        0,    0,            ec_none,
  232.     "substitute",   EX_SUBSTITUTE,  1,    0,            ec_nonalnum,
  233.     "suspend",        EX_SUSPEND,        0,    0,            ec_none,
  234.  
  235.     "t",        EX_COPY,        1,    0,            ec_line,
  236.     "tag",        EX_TAG,        0,    EC_EXCLAM,        ec_1string,
  237.  
  238.     "unabbreviate", EX_UNABBREV,    0,    0,            ec_strings,
  239.     "undo",        EX_UNDO,        1,    0,            ec_none,
  240.     "unmap",        EX_UNMAP,        0,    EC_EXCLAM,        ec_strings,
  241.  
  242.     "v",        EX_V,        1,    0,            ec_nonalnum,
  243.     "version",        EX_VERSION,        0,    0,            ec_none,
  244.     "visual",        EX_VISUAL,        0,    EC_EXCLAM|EC_EXPALL,    ec_1string,
  245.  
  246.     "wn",        EX_WN,        0,    EC_EXCLAM,        ec_none,
  247.     "wq",        EX_WQ,        0,    EC_EXCLAM|EC_EXPALL,    ec_1string,
  248.     "write",        EX_WRITE,        1,    EC_EXCLAM|EC_EXPALL,    ec_1string,
  249.  
  250.     "xit",        EX_XIT,        0,    0,            ec_none,
  251.  
  252.     "yank",        EX_YANK,        0,    0,            ec_none,
  253.  
  254.     "z",        EX_Z,        0,    0,            ec_none,
  255.  
  256.     "|",        EX_GOTO,        0,    0,            ec_none,
  257.     "~",        EX_TILDE,        0,    0,            ec_rest,
  258.  
  259.     NULL,        0,            0,    0,            ec_none,
  260. };
  261.  
  262. /*
  263.  * Internal routine declarations.
  264.  */
  265. static    int    decode_command P((char **, bool_t *, struct ecmd **));
  266. static    bool_t    get_line P((char **, Line **));
  267. static    bool_t    get_range P((char **));
  268. static    void    badcmd P((bool_t, char *));
  269. static    char    *show_line P((void));
  270. static    char    *expand_percents P((char *));
  271.  
  272. /*
  273.  * These are used for display mode.
  274.  */
  275. static    Line    *curline;
  276. static    Line    *lastline;
  277. static    bool_t    do_line_numbers;
  278.  
  279. /*
  280.  * Macro to skip over whi